//
//  NSDate+Extension.swift
//  SinaWeibo
//
//  Created by yang on 15/11/22.
//  Copyright © 2015年 yang. All rights reserved.
//

import Foundation
extension NSDate{
    class func sinaDate(dateString:String) -> NSDate? {
        let dateFormatter = NSDateFormatter()
        dateFormatter.locale = NSLocale(localeIdentifier: "en")
        dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        let date = dateFormatter.dateFromString(dateString)
        return date
    }

    func fullDescription() -> String{
        let calendar = NSCalendar.currentCalendar()
        var dateFormatString = ""
        if calendar.isDateInToday(self){
            let delta = Int(NSDate().timeIntervalSinceDate(self))
            if delta < 60{
            return "刚刚"
            }else if delta < 60 * 60 {
            return "\(delta / 60)分钟前"
            }
            return "\(delta / 3600)小时前"
        }else{
            if calendar.isDateInYesterday(self){
                dateFormatString = "昨天 HH:mm"
            }else{
                let result = calendar.components(.Year, fromDate: self, toDate: NSDate(), options: [])
                if result.year == 0 {
                    dateFormatString = "MM-dd HH:mm"
                }else{
                    dateFormatString = "yyyy-MM-dd HH:mm"
                }
            }
            let df = NSDateFormatter()
            df.dateFormat = dateFormatString
            return df.stringFromDate(self)
        }
    }
}